//
//  LWXHomePageController.m
//  EPA
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 Liuwx. All rights reserved.
//

#import "LWXHomePageController.h"
#import "CK_MenuViewController.h"
#import "LWXNetWorkRequestManager.h"
#import "LWXCollectionViewCell.h"
#import "LWXCycleView.h"
#import "LWXHomeDetailViewController.h"
#import "LWXRadioController.h"
#import "LWXProductsController.h"
#import "LWXTopicsController.h"
#import "UserinfoManager.h"
@interface LWXHomePageController ()<MenuClickDelegate,UICollectionViewDataSource,UICollectionViewDelegate>

// CollectionView
@property (nonatomic, strong) UICollectionView *colleltionView;
// 数组属性
@property (nonatomic, strong) NSMutableArray *dataSource;

/// 轮播图属性
@property (nonatomic, strong) LWXCycleView *cycleView;

@end

@implementation LWXHomePageController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [CK_MenuViewController getMenuViewController].menuDelegate = self;
    if ([UserinfoManager shareManager].coverimg) {
        [CK_MenuViewController getMenuViewController].name = [UserinfoManager shareManager].coverimg;
    }else {
        [CK_MenuViewController getMenuViewController].name = @"头像.jpg";
    }
    
    
    [CK_MenuViewController getMenuViewController].menuDelegate = self;
    [CK_MenuViewController getMenuViewController].menuArray = [@[@"今日 · TODAY", @"电台 · RADIO", @"阅读 · REASING", @"话题 · TOPICS", @" 音乐 · MUSIC", @"良品 · PRODUCTS", @"订阅 · SUBSCRIPTION", @"设置 · SETTING"] mutableCopy];
    
    self.dataSource = [NSMutableArray new];
    
    [LWXNetWorkRequestManager requestURL:[NSURL URLWithString:READLIST_URL] Method:LWX_GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            NSDictionary *dataDict = jsonDict[@"data"];
           
            for (NSDictionary *itemDict in dataDict[@"list"]) {
                LWXHomeModel *model = [LWXHomeModel new];
               
                [model setValuesForKeysWithDictionary:itemDict];
                [self.dataSource addObject:model];
                
            }
            
             NSMutableArray *image = [NSMutableArray new];
            for (NSDictionary *itemDict in dataDict[@"carousel"]) {
                [image addObject:itemDict[@"img"]];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                self.cycleView.imgNameArray = image;
                
                [self.colleltionView reloadData];
            });
        }

    }];
    
   // 创建 collectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.colleltionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    //设置 layout
    layout.itemSize = CGSizeMake(self.view.frame.size.width / 3, (self.view.frame.size.height - 264) / 3);
    
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 200);
    
    layout.minimumInteritemSpacing =0;
    layout.minimumLineSpacing = 0;
    self.colleltionView.dataSource = self;
    self.colleltionView.delegate = self;
    self.colleltionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.colleltionView];
    //注册 cell
    
    [self.colleltionView registerNib:[UINib nibWithNibName:@"LWXCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    //注册头
    [self.colleltionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    //创建轮播图
    self.cycleView = [[LWXCycleView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) interVal:3];
    
}

#pragma mark - UICollectionViewDelegate
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        [header addSubview:self.cycleView];

        return header;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    LWXHomeDetailViewController *detailVC = [[LWXHomeDetailViewController alloc] init];
    LWXHomeModel *model = self.dataSource[indexPath.row];
    detailVC.type = [NSString stringWithFormat:@"%ld",model.type];
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
}




#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LWXCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.model = self.dataSource[indexPath.row];
  
    return cell;
}


//点击抽屉按钮
- (IBAction)didClickLeftBarBtnItem:(id)sender {

    if (![CK_MenuViewController getMenuViewController].isShowing) {
        [[CK_MenuViewController getMenuViewController] showLeftViewController];
    }else {
        [[CK_MenuViewController getMenuViewController] hideLeftViewController];
    }
    
    
}

// 点击菜单实现协议方法
- (void)didClickMenuIndex:(NSInteger)index Title:(NSString *)title {
     [self.navigationController popToRootViewControllerAnimated:NO];
    
    switch (index) {
           
        case 1:
        {
            LWXRadioController *radioVC = [[LWXRadioController alloc]init];
            [self.navigationController pushViewController:radioVC animated:YES];
        }
            break;
            
        case 3:
        {
            LWXTopicsController *topicsTopics = [[LWXTopicsController alloc]init];
            [self.navigationController pushViewController:topicsTopics animated:YES];
        }
            break;
            
        case 5:
        {
            LWXProductsController *productsVC = [[LWXProductsController alloc]init];
            [self.navigationController pushViewController:productsVC animated:YES];
        }
            break;
            
            
        default:
            break;
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
